//
//  WDCompanyAddressController.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/11/30.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDCompanyAddressController.h"
#import "WDLinkMessageCell.h"
#import "WDLinkMessage.h"
#import "WDFirstSort.h"
#import "WDAddressDetailController.h"
#import "WDPlaceholderView.h"

@interface WDCompanyAddressController () <UITableViewDelegate, UITableViewDataSource>
/** 初始联系人数据 */
@property (nonatomic, strong) NSMutableArray *linkMessages;
/** 索引数据（按姓名排序） */
@property (nonatomic, strong) NSArray *data;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 占位图 */
@property (strong, nonatomic) WDPlaceholderView *placeView;

@end

@implementation WDCompanyAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    if ([self respondsToSelector:@selector(clickNavLeft)]) {
        
        [self loadNavItemWithAction:@selector(clickNavLeft) btnImage:@"actionbar" btnSelImage:@"actionbar" title:@"企业通讯录"];
    }
    
    [self setUpTableView];
    
    [self showLoadView];
    
    //解档联系人信息
    [self getAddressList];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)getAddressList
{
    //解档联系人信息
    NSString *path = [WDCachePath stringByAppendingPathComponent:WDAddressFile];
    
    self.linkMessages = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    //排序联系人
    self.linkMessages = (NSMutableArray *)[self.linkMessages sortedArrayUsingComparator:^NSComparisonResult(WDLinkMessage *obj1,WDLinkMessage *obj2) {
        
        return [obj1.start_with compare:obj2.start_with];
        
    }];
    
    //分好组的字典数组
    NSArray *linkMessageArray = [WDFirstSort firstSortWithObjectArray:self.linkMessages];
    
    self.data = linkMessageArray;
    
    [self hideLoadView];
    
    [self.tableView reloadData];
}

#pragma mark - 加载控件
//加载占位图
- (void)showLoadView
{
    self.placeView = [WDPlaceholderView placeholderViewWithImageName:@"icon_loading" status:@"努力加载中..."];
    [self.view addSubview:self.placeView];
    
    [self.placeView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(100);
        make.left.right.bottom.equalTo(0);
    }];
}

//移除占位图
- (void)hideLoadView
{
    [self.placeView removeFromSuperview];
}

- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, WDScreenH - 64) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.sectionIndexColor = ColorForRGB(0x6b6b6b);
    
    tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark - <tableViewDataSource>
//返回每组有几项
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dict = self.data[section];
    NSMutableArray *array = dict[@"content"];
    return array.count;
}

//返回有几组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.data count];
}

//返回每一行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WDLinkMessageCell *cell = [WDLinkMessageCell linkMessageCellWithTableView:tableView];
    
    //遍历数组，显示排序后的数据
    NSDictionary *dict = self.data[indexPath.section];
    NSMutableArray *linkMessages = dict[@"content"];
    
    cell.linkMessage = linkMessages[indexPath.row];
    
    return cell;
}

#pragma mark - <UITaleViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //点击跳转到通讯录的详情页
    WDAddressDetailController *tempV = [[WDAddressDetailController alloc] init];
    
    NSDictionary *dict = self.data[indexPath.section];
    NSMutableArray *linkMessages = dict[@"content"];
    
    WDLinkMessage *tempLinkMessage = linkMessages[indexPath.row];
    tempV.linkMessage = tempLinkMessage;
    tempV.subordinate = [tempLinkMessage.is_responsible boolValue];
    
    tempV.view.backgroundColor = WDBackColor;
    [self.navigationController pushViewController:tempV animated:YES];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSDictionary *dict in self.data) {
        NSString *title = dict[@"firstLetter"];
        [resultArray addObject:title];
    }
    return resultArray;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WDScreenW, 20)];
    headView.backgroundColor = ColorForRGB(0xf0f1f2);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, WDScreenW, 20)];
    
    NSDictionary *dict = self.data[section];
    NSString *title = dict[@"firstLetter"];
    label.text = title;
    label.font = [UIFont systemFontOfSize:15];
    label.textColor = ColorForRGB(0x6b6b6b);
    [headView addSubview:label];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    //这里是为了指定索引index对应的是哪个section的，默认的话直接返回index就好。其他需要定制的就针对性处理
    
    //以下e.g.实现了将索引0对应到section 1里。其他的正常对应。
    
    //    if (index == 0) {
    //        return 1;
    //    }
    //    return index;
    
    return index;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

@end
